package com.kaluli.project.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class NSHGorder {
	
	String localTmp = "/data/xml/";
	String fileName = "880020201606041120052482";
	String destFileName = localTmp + "/" + fileName + ".xml";
	String text = "1_880020201605241620010110";
	String textName = localTmp + "/" + text + ".txt";

	public static void main(String[] args) {
		System.out.println("运行开始!!");
		NSHGorder o = new NSHGorder();
//		o.getReceiveCon();
		
		o.sendFile();
		System.out.println("运行结束!!");
	}
	
	//OrderGoodTotal价钱
	//OrderStatus状态 C(取消) S(创建)
	public void sendFile(){
		
		File tmpFtpFile = null;
		String xb="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<Manifest><Head><MessageID>880020201606041120052482</MessageID><MessageType>880020</MessageType><SenderID>IE160122481431</SenderID><SendTime>2016-06-04 03:20:05</SendTime><Version>1.0</Version></Head><Declaration><EOrder><OrderId>KLL20160604056110311024388</OrderId><IEFlag>I</IEFlag><OrderStatus>S</OrderStatus><EntRecordNo>IE160122481431</EntRecordNo><EntRecordName>虎扑（上海）文化传播股份有限公司</EntRecordName><OrderName>魏启赞</OrderName><OrderDocType>01</OrderDocType><OrderDocId>350423198308056514</OrderDocId><OrderPhone>18657980522</OrderPhone><OrderGoodTotalCurr>142</OrderGoodTotalCurr><Freight>0.0</Freight><FreightCurr>142</FreightCurr><Tax>51.062900000000006</Tax><TaxCurr>142</TaxCurr><OrderGoodTotal>499.0</OrderGoodTotal><Note></Note><OrderDate>2016-06-04 03:03:10</OrderDate></EOrder><EOrderGoods><EOrderGood><GNo>1</GNo><ChildOrderNo>KLL20160604056110311024388</ChildOrderNo><StoreRecordNo>IE160122481431</StoreRecordNo><StoreRecordName>虎扑（上海）文化传播股份有限公司</StoreRecordName><CustomsListNO>10025017</CustomsListNO><DecPrice>499.0</DecPrice><Unit>007</Unit><GQty>1</GQty><DeclTotal>499.0</DeclTotal><Notes></Notes></EOrderGood></EOrderGoods></Declaration></Manifest>";
		try {
			// destFileName = localTmp + File.separator + fileName + ".xml";
			
			tmpFtpFile = new File(destFileName);
			tmpFtpFile.createNewFile();
			if (tmpFtpFile.exists()) {
				BufferedWriter out = new BufferedWriter(new FileWriter(tmpFtpFile));
				out.write(AESMessageSigner.getInstance().encrypt(xb.toString().getBytes()));
				out.close();
				/*
				OutputStreamWriter writerStream = new OutputStreamWriter(new FileOutputStream(tmpFtpFile), "UTF-8");
				BufferedWriter out = new BufferedWriter(writerStream);

				out.write(xb.toString());
				out.close();*/
			}

		} catch (IOException e) {
			
		}
	}
	
	public void getReceiveCon(){
		try{
			InputStream fis = new FileInputStream(new File(textName));
			String content = convertStreamToString(fis);
			fis.close();
			content = AESMessageSigner.getInstance().decrypt(content);
			System.out.println(content);
		}catch(Exception e){
			
		}
		
	}
	
	private static String convertStreamToString(InputStream is){
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuffer sb = new StringBuffer();
		String line = null;
		try{
			while((line=reader.readLine())!=null){
				sb.append(line);
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try {
				is.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
		return sb.toString();
	}

}
